import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/routes/routes.dart';
import 'package:sainbainu/common/services/user.dart';
import 'package:sainbainu/common/values/colors.dart';

import 'index.dart';

class AccountSecurityPage extends GetView<AccountSecurityController> {
  const AccountSecurityPage({Key? key}) : super(key: key);

  /// 分割线
  Widget _buildDivider() {
    return const Divider(
      height: 0,
      indent: 16,
      endIndent: 16,
      thickness: 0.5,
      color: AppColors.border,
    );
  }

  // 菜单每一项
  Widget _buildCellItem({
    required String title,
    Widget? rightChild,
    bool showArrow = true,
    void Function()? onTap,
  }) {
    return ListTile(
      title: Text(title),
      trailing: SizedBox(
        width: Get.width / 2,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            if (rightChild != null) rightChild,
            if (showArrow) const Icon(Icons.keyboard_arrow_right),
          ],
        ),
      ),
      tileColor: Colors.white,
      enableFeedback: true,
      contentPadding: const EdgeInsets.symmetric(horizontal: 16),
      onTap: onTap,
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AccountSecurityController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: Text("账号与安全".tr)),
          body: SingleChildScrollView(
            physics: const BouncingScrollPhysics(),
            child: Column(
              children: [
                _buildCellItem(
                  title: '绑定手机'.tr,
                  rightChild: Obx(
                    () => Text(
                      _.state.obscureMobile.value,
                      style: const TextStyle(
                        fontSize: 14,
                        color: AppColors.unactive,
                      ),
                    ),
                  ),
                  onTap: () => Get.toNamed(AppRoutes.bindMobile),
                ),
                _buildDivider(),
                _buildCellItem(
                  title: '密码设置'.tr,
                  onTap: () => Get.toNamed(AppRoutes.passwordChange),
                ),
                _buildDivider(),
                _buildCellItem(
                  title: '第三方账号绑定'.tr,
                  onTap: () => Get.toNamed(AppRoutes.bindThirdParty),
                ),
                _buildDivider(),
                _buildCellItem(
                  title: '实名认证'.tr,
                  onTap: () => _.toRealNameAuth(),
                  rightChild: Obx(
                    () => Text(
                      style: const TextStyle(
                        fontSize: 14,
                        color: AppColors.unactive,
                      ),
                      _.state.isRealed.value ? '已认证' : '去认证',
                    ),
                  ),
                ),
                _buildDivider(),
                _buildCellItem(
                  title: '官方认证'.tr,
                  onTap: () => _.toOfficialAuth(),
                  rightChild: Obx(
                    () => Text(
                      style: const TextStyle(
                        fontSize: 14,
                        color: AppColors.unactive,
                      ),
                      _.state.isOfficial.value ? '已认证' : '去认证',
                    ),
                  ),
                ),
                _buildDivider(),
                _buildCellItem(
                  title: 'Registration ID',
                  showArrow: false,
                  onTap: () => _.handleCopy(),
                  rightChild: Row(
                    children: [
                      Obx(
                        () => Text(
                          _.state.registrationId.value,
                          style: const TextStyle(
                            fontSize: 14,
                            color: AppColors.unactive,
                          ),
                        ),
                      ),
                      const SizedBox(width: 5),
                      const Icon(Icons.copy_rounded, size: 18),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
